﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using System.Text;

public partial class RecvFile_EditRecvFileDlg : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utils.ParseIntQueryString(Request, "id", true, ref recvFileId))
        {
            lblMsg.Text = "参数不正确";
            return;
        }

        if (!IsPostBack)
        {
            DoDataBind();
        }
        else
        {
            LoadDetailControl();
        }
    }

    int recvFileId;
    bool IsInstructionRecvFile
    {
        get { return ViewState["IsInstructionRecvFile"] == null ? false : (bool)ViewState["IsInstructionRecvFile"]; }
        set { ViewState["IsInstructionRecvFile"] = value; }
    }

    private void DoDataBind()
    {
        IRecvFileManager rfMgr = ManagerFactory.CreateRecvFileManager();
        RecvFileInfo rf = rfMgr.GetRecvFile(CallContext, recvFileId);
        if (rf != null)
        {
            IRecvFileEditorCtrl rfCtrl = null;
            IsInstructionRecvFile = RecvFileUtils.IsInstructionRecvFile(rf);
            rfCtrl = LoadDetailControl();
            rfCtrl.EditRecvFile(rf);
        }
        else
        {
            lblMsg.Text = "收文不存在或已被删除，请刷新列表后重试。";
            btOK.Enabled = false;
            return;
        }
    }

    IRecvFileEditorCtrl LoadDetailControl()
    {
        Control ctrl;
        if (IsInstructionRecvFile)
        {
             ctrl = LoadControl("~/RecvFile/ucInstructionRecvFileDetail.ascx");
        }
        else
        {
            ctrl = LoadControl("~/RecvFile/ucRecvFileDetail.ascx");
        }

        phDetail.Controls.Add(ctrl);
        return ctrl as IRecvFileEditorCtrl;
    }

    protected void btOK_Click(object sender, EventArgs e)
    {
        IRecvFileManager rfMgr = ManagerFactory.CreateRecvFileManager();
        RecvFileInfo rf = rfMgr.GetRecvFile(CallContext, recvFileId);
        if (rf != null)
        {
            IRecvFileEditorCtrl rfCtrl = phDetail.Controls[0] as IRecvFileEditorCtrl;
            if (rfCtrl.GetRecvFileInfo(rf))
            {
                rf.AID = recvFileId;
                rfMgr.UpdateRecvFile(CallContext, rf);
                lblMsg.Text = "更新成功, 3秒钟将关闭对话框。";
                GenerateCloseDialogScript();
            }
            else
            {
                lblMsg.Text = rfCtrl.ErrorMessage;
            }
        }
        else
        {
            lblMsg.Text = "收文不存在或已被删除，请刷新列表后重试。";
            btOK.Enabled = false;
            return;
        }
    }

    void GenerateCloseDialogScript()
    {
        StringBuilder sbscript = new StringBuilder();
        sbscript.Append("setTimeout(function(){window.parent.document.onDialogCallBack(1, 'succeed',");
        sbscript.Append("null);}, 3000);");

        btOK.Enabled = false;
        ClientScript.RegisterStartupScript(GetType(),
            "BCF145CA-3290-43A0-9543-D4EB12506FB9",
            sbscript.ToString()
            , true);
    }
}
